// Use JS for the server because we also want Rhino for batch compiles.
// We don't want Java because we don't want to require a compile of our code.
// Eventually switch this to Rio.
// Would we ship with a precompiled Rio runner/compiler as well as source?
// Maybe eventually even compile the Rio test server directly to JVM.

// Use Jetty because it's small and well-maintained.
// Winstone is another option to consider. It's smaller but not as well-maintained.

importPackage(Packages.org.eclipse.jetty.server);
importPackage(Packages.org.eclipse.jetty.server.handler);

// TODO Is there a way to find the script file path in Rhino? If so, we can avoid a dependency on the argument being passed in.
// TODO At least test to see if we have the arg. If not, check '.' and '..' for the project dir.
var rootDir = arguments[0];
print("Starting server for: " + rootDir);

var server = new Server(8080);
var handler = new ResourceHandler();
handler.directoriesListed = true;
handler.resourceBase = rootDir;
server.handler = handler;
server.start();
server.join();

/*
// Jetty's Hello World example modified for Rhino.
// Kept here for future reference in case I need to customize anything.
importPackage(Packages.javax.servlet.http);
server.setHandler(
  new JavaAdapter(AbstractHandler, {
    handle: function(target, baseRequest, request, response) {
      response.setContentType("text/html;charset=utf-8");
      response.setStatus(HttpServletResponse.SC_OK);
      baseRequest.setHandled(true);
      response.getWriter().println("<h1>Hello World</h1>");
    }
  })
);
*/
